const app = getApp()
Page({
  /*
   *
   * 页面的初始数据
   */

  data: {
      publicUrl: app.globalData.publicUrl,
      phone:'',
      password:[
        {
          password: "******",
        }
      ],
      avatar:'',
      id:'',
      typename:'nickname' ,
      typephone:'mobile',
    //底部导航
    navbar1: [
      {
        name: '广场',
        icon: '/pages/image/squire2.png'
      },
    ],
    navbar2: [
      {
        name: '我的',
        icon: '/pages/image/our1.png'
      }
    ],
  },
  tableft: function () {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  tabright:function(){
    wx.navigateTo({
      url: '/pages/person/person',
    })
  },
  getPhoneNumber: function (e) {
    if (e.detail.errMsg == "getPhoneNumber:fail user deny") return;
    //用户允许授权
    // console.log(e.detail.errMsg)
    // console.log(e.detail.iv)
    // console.log(e.detail.encryptedData)
    //  wx.showLoading()

    var self = this
    //1. 调用登录接口获取临时登录code
    wx.login({
      success: res => {
        //console.log(res.code);
        if (res.code) {
          //2. 访问登录凭证校验接口获取session_key、openid
          wx.request({
            url: self.data.publicUrl + 'session_key',
            // url:'http://192.168.1.153/wan/public/api/ceshi/session_key',
            data: {
              appid: "wx4eec067070a57604",
              secret: "ab3c1e2766cfc6bf02d18543baf44bba",
              js_code: res.code,
              grant_type: "authorization_code"
            },
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (data) {
              console.log(data);
              if (data.statusCode == 200) {
                wx.request({
                  url: self.data.publicUrl + 'phone',
                  // url: 'http://192.168.1.153/wan/public/api/ceshi/phone',
                  data: {
                    encryptedData: e.detail.encryptedData,
                    appid: 'wx4eec067070a57604',
                    session_key: data.data,
                    iv: e.detail.iv,
                    uid: wx.getStorageSync('uid'),
                  },
                  method: 'POST',
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  success: function (data2) {
                    //   wx.hideLoading()
                    console.log('手机号时:')
                    console.log(data2)
                    if (data2.statusCode == 200 && data2.data.phoneNumber) {

                      self.setData({
                        phone: data2.data.phoneNumber
                      })
                      self.onShow();
                    }
                  },
                  fail: function (err) {

                    console.log(err);
                  }
                })
              }
            },
            fail: function (err) {
              console.log('拒绝了');
              console.log(err);
            }
          })
        }

      }
    })
  },
  changeAvatar: function () {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            // 相册
            _this.chooseWxImage('album')
            //拍照
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage: function (type) {
    var  userid = wx.getStorageSync('user_id')
    let that = this;
  
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      sourceType: [type],
      success: function (res) {
        console.log(res.tempFilePaths[0]);
        that.setData({
          avatar: res.tempFilePaths[0],
        })
        wx.uploadFile({
          url: that.data.publicUrl + 'user_img',
          filePath:res.tempFilePaths[0],
          name: 'file',
          formData: {
            id:wx.getStorageSync('uid')
          },
          success:function(res){
            console.log(res);
            // console.log(res.data.message);
            if (res.statusCode=="200"){
              console.log('上传成功');
              that.onShow();
              // //跳转路径
              // wx.redirectTo({   
              //   url: 'private?id=userid'
              // })
          }
          },

          fial:function(){
            console.log('上传失败')
          }
        })
        // wx.uploadFile({
        //   url: 'http://192.168.1.131:8080/friends/user/upload.action' ,
        //   filePath: res.tempFilePaths[0],
        //   formData:{
        //     id: userid
        //   },
        //   name: 'file',
          
        //   success: function (res) {
        //     console.log('上传成功')
        //     console.log(res)
        //   },
        //   fail:function(){
        //     console.log('上传失败')
        //   }
        // })
      }
    })
  },
  /* nickname:function(){
    wx.navigateTo({
      url: '/pages/private/nickname?id=' + ids + '&type=' + types,
     
    })
  }, */
  /* changeAvatar: function () {
    var that = this;
    wx.chooseImage({
      count: 1, 
      sizeType: ['compressed'], 
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.uploadFile({
          url: that.data.API_URL + 'uploadavatarurl',
          filePath: res.tempFilePaths[0],
          name: 'file',
      
          success: function (info) {
            console.log(info);
            that.setData({
              'myInfo.wx_avatarurl': res.tempFilePaths[0]
            });
            wx.setStorageSync('wx_avatarurl', res.tempFilePaths[0]);
          }
        })
      },
      fail: function () {
   
      },
      complete: function () {
     
      }
    })
  }, */
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
  
 
    // wx.request({
    //   url:'http://192.168.1.131:8080/friends/user/selectUserById.action',
    //   method:'POST',
    //   header: { 'content-type': 'application/x-www-form-urlencoded'},
    //   data:{
      
    //     id: options.id
    //   },
    //   success:function(res){
    //     console.log(res.data)
    //     that.setData({
    //       list:[res.data]
    //     })
    //   },
    //   fail:function(res){
    //     console.log('失败')
    //   }
    // })
   // var ids = options.id
    if (options.id){
      this.setData({
        id: options.id
      })
    }
   
 
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取数据库里的信息在页面显示
    var that = this
    wx.request({
      url: that.data.publicUrl +'user_info',
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        // id:options.id
        id: wx.getStorageSync('uid')
      },
      success: function (res) {
        console.log('24244');
        console.log(res)
        that.setData({
          list: res.data.data,
          avatar: res.data.data.img
        })
      },
      fail: function (res) {
        console.log('失败')
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})